unit URelatorioCentroCusto;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ComCtrls, Db, DBTables,Printers, StdCtrls, Buttons, ExtCtrls,ShellAPI;

type
  TFRelatorioCentroCusto = class(TForm)
    TreeView1: TTreeView;
    Panel1: TPanel;
    BtnImprimir: TBitBtn;
    BtnFechar: TBitBtn;
    BtnExpandir: TBitBtn;
    QCentro: TQuery;
    BitBtn1: TBitBtn;
    PSetupDialog: TPrinterSetupDialog;
    CBImpressora: TComboBox;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TreeView1DblClick(Sender: TObject);
    procedure BtnFecharClick(Sender: TObject);
    procedure BtnImprimirClick(Sender: TObject);
    procedure BtnExpandirClick(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
  private
    { Private declarations }
   procedure MontaArvore;
   procedure carrega;
   Procedure PegaImpresoras;
  public
    { Public declarations }

  end;

var
  FRelatorioCentroCusto: TFRelatorioCentroCusto;
  FirstNo, no :TTreeNode;

implementation

uses UGeral;

{$R *.DFM}

Procedure TFRelatorioCentroCusto.PegaImpresoras;
begin
 CBImpressora.Items.Clear;
 CBImpressora.Items.Assign(printer.Printers);
end;

procedure TFRelatorioCentroCusto.FormCreate(Sender: TObject);
begin
 MontaArvore;
 PegaImpresoras;
end;

procedure TFRelatorioCentroCusto.MontaArvore;
var
 dono :TTreeNodes;
begin
 TreeView1.Items.Clear;
 Qcentro.close;
 Qcentro.Open;
 Qcentro.First;
 dono := TTreeNOdes.Create(TreeView1);
 no := TTreeNode.Create(dono);
 FirstNo := TreeView1.Items.Add (no,QCentro.fieldbyname('Estrutura').AsString + '-' + Qcentro.fieldbyname('Descricao').AsString);
 TreeView1.Items.Item[0].Data:=Qcentro.GetBookmark;
 Qcentro.Next;
 carrega;
end;

procedure TFRelatorioCentroCusto.carrega;
var
  nofilho :TTreeNode;
  codPai,Cod,i,Pai :Integer;
  S   :String;
  Ch :Boolean;
begin
 While not (Qcentro.EOF) do
  begin
   Cod:=Qcentro.fieldbyname('Codigo').AsInteger;
   codPai := Qcentro.fieldbyname('CodPai').AsInteger;
   if pai <> CodPai then
    begin
     Qcentro.Locate('Codigo',codPai,[LoCaseInsensitive]);
     TreeView1.Items.GetFirstNode;
     i:=0;
     S:=Qcentro.FieldByName('Estrutura').AsString+Qcentro.FieldByName('Descricao').asString;
     ch:=false;
     while (i < TreeView1.Items.Count - 1 ) and Not(CH) do
      begin
       Qcentro.GotoBookmark(TreeView1.Items.Item[i].Data);
       ch:=POs(s,Qcentro.FieldByName('Estrutura').AsString + Qcentro.FieldByName('Descricao').asString) <> 0;
       if (i < TreeView1.Items.Count - 1) and Not(CH) then
        i:=i+1;
      end;
     no:=TreeView1.Items.Item[i];
     Qcentro.Locate('Codigo',cod,[LoCaseInsensitive]);
    end;
   nofilho:=TreeView1.Items.AddChild (no, copy (Qcentro.fieldbyname('Estrutura').Asstring,Length(Qcentro.fieldbyname('Estrutura').asString)-2,3)  + '- ' + Qcentro.FieldByName('Descricao').asString);
   nofilho.Data:=Qcentro.GetBookmark;
   if pai <> CodPai then
    begin
     pai:=codpai;
    end;
   Qcentro.Next;
   end
end;

procedure TFRelatorioCentroCusto.FormClose(Sender: TObject; var Action: TCloseAction);
begin
 Action:=caFree;
end;

procedure TFRelatorioCentroCusto.TreeView1DblClick(Sender: TObject);
begin
 Self.close;
end;

procedure TFRelatorioCentroCusto.BtnFecharClick(Sender: TObject);
begin
 Self.Close;
end;

procedure TFRelatorioCentroCusto.BtnImprimirClick(Sender: TObject);
Var
 F,P :TextFile;
 s   :String;
begin
 try
  if FileExists(GetCurrentDir + '\CentroCusto.txt') then
   DeleteFile(GetCurrentDir + '\CentroCusto.txt');
  TreeView1.SaveToFile(GetCurrentDir + '\CentroCusto.txt');
  AssignFile(f,GetCurrentDir + '\CentroCusto.txt');
  Reset(F);
  if CBImpressora.Text <> '' then
   Printer.PrinterIndex:=CBImpressora.ItemIndex;
  AssignPrn(P);
  Rewrite(P);
  while not(eof(f))do
   begin
    readln(f,s);
    writeln(p,s);
   end;
  CloseFile(P);
  CloseFile(F);
 except
 end;
end;

procedure TFRelatorioCentroCusto.BtnExpandirClick(Sender: TObject);
var
 i : Longint;
begin
 for i:=0 to TreeView1.Items.Count - 1 do
  begin
   TreeView1.Items.Item[i].Expand(true);
  end;
end;

procedure TFRelatorioCentroCusto.BitBtn1Click(Sender: TObject);
begin
 PSetupDialog.Execute;
 PegaImpresoras;
end;

end.
